home *** CD-ROM | disk | FTP | other *** search
/ SuperHack / SuperHack CD.bin / CODING / MISC / MAG02.ZIP / MAG02.TXT
Encoding:
Text File  |  1995-10-27  |  36.0 KB  |  780 lines

  1. Spellcaster presents:
  2.  
  3.  
  4. TTTTTTTTTT HH      HH EEEEEEEEEE    MM      MM    AAAA     GGGGGGGGG
  5.     TT     HH      HH EE            MMM    MMM   AA  AA   GG
  6.     TT     HH      HH EE            MM M  M MM  AA    AA  GG  
  7.     TT     HHHHHHHHHH EEEEEE        MM  MM  MM  AAAAAAAA  GG   
  8.     TT     HH      HH EE            MM      MM  AA    AA  GG    GGGG
  9.     TT     HH      HH EE            MM      MM  AA    AA  GG      GG
  10.     TT     HH      HH EEEEEEEEEE    MM      MM  AA    AA   GGGGGGGG
  11.  
  12.                                                         Issue 2
  13.                                                         27-9-95
  14.  
  15.  
  16.  
  17. ■ Index
  18.  
  19.         1. Introduction
  20.           1.1. About the magazine
  21.           1.2. About the author
  22.           1.3. Distribution
  23.           1.4. Contribuitions
  24.           1.5. Hellos and greets
  25.         2. Conditions: Can you live without them ?
  26.           2.1. What are they ?
  27.           2.2. How can I use them ?
  28.         3. The mistery of the lost ASCII
  29.         4. Assembly and Pascal
  30.           4.1. What is Assembly ?
  31.           4.2. The BIOS
  32.           4.3. Interrupts
  33.           4.4. Segments and Offsets
  34.         5. Graphics, part 1 : Mode 13h
  35.           5.1. Introduction
  36.           5.2. What's mode 13h ?
  37.           5.3. How do I get into mode 13h ?
  38.           5.4. Putting Down a Pixel
  39.         6. Points of View
  40.         7. The adventures of Spellcaster, part 2.
  41.  
  42.  
  43. ■ Introduction
  44.  
  45.   ■ About the magazine
  46.  
  47.     Welcome to the second issue of 'The Mag', the electronic magazine, brought
  48.   to you by Spellcaster, alias Diogo de Andrade.
  49.     You may be wondering why did I write this issue so close to the last one.
  50.   The answear is simple: I just felt like it... I wanted to write, so, there
  51.   you go.
  52.     This issue has a new regular article. It is called 'Graphics' and it's about
  53.   programming the PC's VGA board (in the 320x200x256 resolution).
  54.     This magazine is dedicated to all the programmers and would-be programmers
  55.   out there, and to those who wish to learn how to program anything, from demos
  56.   to games, passing through utilities and all sort of thing your mind can think
  57.   of.
  58.  
  59.     When you read this magazine, I'll assume some things. First, I assume you
  60.   have Borland's Turbo Pascal, version 6 and upwards. I'll also think you have
  61.   a 80386 (or 386 for short; a 486 would be even better), a load of patience
  62.   and a sense of humor. This last is almost essencial, because I don't receive
  63.   any money for doing this, so I must have fun doing it. I will also take for
  64.   certain you have the 9th grade (or equivelent).
  65.  
  66.     As I stated above, this magazine will be made especially for those who don't
  67.   know where to get information, or want it all in the same place, and to those
  68.   who want to learn how to program, so I'll try to build knowledge, building up
  69.   your skills issue by issue. If you sometimes fail to grasp some concept, don't
  70.   despair; try to work it out.
  71.     That's what I did... Almost everything I know was learnt from painfull
  72.   experience. If you re-re-re-read the article, and still can't understand it,
  73.   just drop a line, by mail, or just plain forget it. Most of the things I 
  74.   try to teach here aren't linked to each other (unless I say so), so if you
  75.   don't understand something, skip it and go back to it some weeks later. It
  76.   should be clearer for you then. Likewise, if you see any terms or words you 
  77.   don't understand, follow the same measures as before.
  78.  
  79.     Ok, as I'm earing the Net gurus and other god-like creatures talking
  80.   already, I'm just going to explain why I use Pascal.
  81.   For starters, Pascal is a very good language, ideal for the beginner, like 
  82.   BASIC (yech!), but it's powerfull enough to make top-notch programms.
  83.   Also, I'll will be using assembly language in later issues, and Pascal makes
  84.   it so EASY to use. 
  85.   Finally, if you don't like my choice of language, you can stop whining. The
  86.   teory behind each article is very simple, and common with any of the main
  87.   languages (C, C++, Assembly - Yes, that's true... BASIC isn't a decent
  88.   language).
  89.  
  90.     Just one last thing... The final part of the magazine is a little story
  91.   made up by my distorted mind. It's just a little humor I like to write, and
  92.   it hasn't got nothing to do with programming (well, it has a little), but, 
  93.   as I said before, I just like to write it.
  94.  
  95.   ■ About the author
  96.  
  97.     Ok, so I'm a little egocentric, but tell me... If you had the trouble of 
  98.   writing hundreds of lines, wouldn't you like someone to know you, even by 
  99.   name ?
  100.  
  101.     My name is Diogo de Andrade, alias Spellcaster, and I'm the creator, 
  102.   editor and writer of this magazine. 
  103.     I live in a small town called Setúbal, just near Lisbon, the capital of
  104.   Portugal... If you don't know where it is, get an encyclopedia, and look for
  105.   Europe. Then, look for Spain. Next to it, there's Portugal, and Setúbal is in
  106.   the middle.
  107.  
  108.     I'm 18 years old, and I just made it in to the university (if you do want
  109.   to know, I'm in the Technical Institute of Lisbon, Portugal), so I'm not 
  110.   a God-Like creature, with dozens of years of practice (I only program by 
  111.   eight years now, and I started in a Spectrum, progressing later to an Amiga.
  112.   I only program in the PC for a year or so), with a mega-computer (I own a 
  113.   386SX, 16 Mhz), that wear glasses with lens that look like the bottom of a 
  114.   bottle (I use glasses, but only sometimes), that has his head bigger than a 
  115.   pumpkin (I have a normal sized head) and with an IQ of over 220 (mine is 
  116.   actually something like 180). I can program in C, C++, Pascal, Assembly 
  117.   and even BASIC (yech!).
  118.  
  119.     So, if I am a normal person, why do I spend time writing this ?
  120.   Well, because I have the insane urge to write thousands of words every now
  121.   and then, and while I'm at it, I may do something productive, like teaching
  122.   someone. I may be young, but I know a lot about computers (how humble I am;
  123.   I know, modesty isn't one of my qualities).
  124.  
  125.     Just one more thing, if you ever program anything, please send to me... I
  126.   would love to see some work you got, maybe I even could learn something with
  127.   it. Also, give me a greet in your program/game/demo... I love seeing my 
  128.   name.
  129.  
  130.   ■ Distribution
  131.  
  132.     I don't really know when can I do another issue, so, there isn't a fixed
  133.   space of time between two issues. General rule, I will try to do one every two
  134.   weeks, maybe more or maybe less.
  135.     'The Mag' is available by the following means:
  136.  
  137.     - Snail Mail : My address is below, in the Contributions seccion... Just
  138.                    send me a disk and tell me what issues you want, and I
  139.                    will send you them...
  140.  
  141.     - E-Mail : If you E-mail me and ask me for some issues, I will Email you
  142.                back with the relevant issues attached.
  143.  
  144.     - BBS's : I don't know for sure what BBS's have or will have my magazine,
  145.               but I will try to post it in the Skyship BBS.
  146.               If you have a BBS and you want to receive 'The Mag', contact me.
  147.  
  148.                  Skyship BBS numbers: (351)+01-3158088
  149.                                       (351)+01-3151435
  150.  
  151.     By the end of this year (1995), I should have an Internet Page, and some
  152.   more BBS's in my list, besides some ftp's.
  153.  
  154.   ■ Contributions
  155.  
  156.     I as I stated before, I'm not a God... I do make mistakes, and I don't 
  157.   have (always) the best way of doing things. So, if you think you've spotted
  158.   an error, or you have thought of a better way of doing things, let me know.
  159.   I'll be happy to receive anything, even if it is just mail saying 'Keep it 
  160.   up'. As all human beings, I need incentive.
  161.  
  162.     Also, if you do like to write, please do... Send in articles, they will be
  163.   welcome, and you will have the chance to see your names up in lights. 
  164.  
  165.     If anyone out there has a question or wants to see an article about 
  166.   something in particular, feel free to write... All letters will be answered,
  167.   provided you give me your address.
  168.  
  169.     I'm also trying to start a new demo/game/utility group, and I need all sort 
  170.   of people, from coders (sometimes, one isn't enough), musicians (I can 
  171.   compose, but I'm a bit limited), graphics artists (I can't draw nothing) and
  172.   spreaders... I mean, by a spreader, someone who spreads things, like this mag.
  173.   If you have a BBS and you want it to include this magazine, feel free to
  174.   write me...
  175.  
  176.     You can also contact me personally, if study on the IST (if you don't
  177.   know what the IST is, you don't study there). I'm the freshman with the 
  178.   black hair and dark-brown eyes... Yes, the pretty one (there goes my modesty
  179.   again). I recommend you to contact me personally, if you can, especially if
  180.   you are a member of the opposite sex (I'm a man, for those of you who are 
  181.   wondering).
  182.  
  183.     My adress is:
  184.                  Praceta Carlos Manito Torres, nº4/6ºC
  185.                  2900 Setúbal
  186.                  Portugal
  187.  
  188.     Email: dgan@rnl.ist.utl.pt
  189.  
  190.  
  191.   ■ Hellos and greets
  192.  
  193.     I'll say hellos and thanks to all my friend, especially for those who put 
  194.   up with my constant whining (you know who you are).
  195.     Special greets go to Denthor from Asphyxia (for the excelent VGA trainers),
  196.   Draeden from VLA (for assembly tutorials), Joaquim Elder Guerreiro, alias
  197.   Dr.Shadow (Delta Team is still up), Alex "Darkfox" (thanks for letting me
  198.   use your BBS), Joäo Neves and Henrique Craveiro for sugestions, and all the
  199.   demo groups out there.
  200.     I also want to say hi to my idols (I know they don't read this, but...),
  201.   Chris Roberts, François Lionet, Archer MacLean, everybody at ID Software and
  202.   Apogee, Sierra On-Line, Lucas Arts and Team 17, for showing me what 
  203.   programming is all about.
  204.  
  205.  
  206.  
  207. ■ Conditions: Can you live without them ?
  208.  
  209.     Answering the question, yes, you can live without them, but no, you can't
  210.   program without them...
  211.  
  212.   ■ What are they ?
  213.  
  214.     Conditions are one of the more important things in Pascal (in any language
  215.   really!). To understand the logics behind conditions, you must understand
  216.   first the concept of TRUE and FALSE, speaking in computer terms.
  217.     The terms TRUE and FALSE in the computer function in the same as our true
  218.   and false, with the diference that we know there is a middle term (the point
  219.   of view). For the computer, there isn't middle term. All there is is TRUE or
  220.   FALSE.
  221.  
  222.   ■ How can use them ?
  223.  
  224.     Ok, this is the base for conditions. Let's supose you wanted to find out
  225.   the biggest number of a group of two numbers.
  226.  
  227.       A:=10;                                 { Give value 10 to var A }
  228.       B:=15;                                 { Give value 15 to var B }
  229.  
  230.       If A>B Then Write('A is biggest');     { This would compare the two }
  231.                                              { numbers, and if A is larger }
  232.                                              { than B, it would write the }
  233.                                              { phrase 'A is biggest. }
  234.  
  235.     Do you see know why conditions are so usefull ?
  236.     Did you understand them ?
  237.     The sintax for the IF keyword is:
  238.  
  239.                IF condition THEN command
  240.  
  241.     This translates in IF the condition is TRUE, THEN execute command.
  242.   The condition can be anything, as long as it gives out a TRUE or FALSE value.
  243.   This is known as boolean algebra, algebra that only gives TRUE or FALSE
  244.   result.
  245.     Following this, here's a list of operations that give boolean results.
  246.  
  247.     ┌──────────┬──────────────┬────────────────────────────────────────────┐
  248.     │ Operator │  Desciption  │  Example                                   │
  249.     ├──────────┼──────────────┼────────────────────────────────────────────┤
  250.     │    =     │   Equal to   │  A=B : Returns TRUE if A equals B          │
  251.     │          │              │                                            │
  252.     │    <>    │ Not Equal to │  A<>B : Returns TRUE if A is diferent to B │
  253.     │          │              │                                            │
  254.     │    <     │  Less than   │  A<B : Returns TRUE if A is less than B    │
  255.     │          │              │                                            │
  256.     │    >     │ Greater than │  A>B : Returns TRUE if A is greater than B │
  257.     │          │              │                                            │
  258.     │    <=    │ Less than or │  A<=B : Returns TRUE if A is less or equal │
  259.     │          │   Equal to   │         to B                               │
  260.     │          │              │                                            │
  261.     │    >=    │ Greater than │  A>=B : returns TRUE is A is greater or    │
  262.     │          │  or Equal to │         equal to B                         │
  263.     └──────────┴──────────────┴────────────────────────────────────────────┘
  264.  
  265.     Notice that when you compare, you do not use the ':' after the variable
  266.   name. Let's do a program to implement this, so that you understand it
  267.   better...
  268.  
  269.   Program Test_6;                                    { Starts Test_6 }
  270.  
  271.   Var A,B:Longint;                                   { Defines vars A and B }
  272.  
  273.   Begin                                              { Starts main block }
  274.        Write('Write number 1:');                     { Writes message }
  275.        Readln(A);                                    { Get value of var A }
  276.        Write('Write number 2:');                     { Writes another message }
  277.        Readln(B);                                    { Get value of var B }
  278.        If A=B then Writeln('The numbers are equal'); { Compares A and B. If }
  279.                                                      { they are equal, then he }
  280.                                                      { writes 'The numbers are }
  281.                                                      { equal on the screen. }
  282.        If A<B then Writeln('A is smaller than B');   { Compares A and B. If }
  283.                                                      { A is less than B, it }
  284.                                                      { writes on the screen }
  285.                                                      { 'A is smaller than B' }
  286.        If A>B then Writeln('A is bigger than B');    { Compares A and B. If }
  287.                                                      { A is greater than B, it }
  288.                                                      { writes on the screen: }
  289.                                                      { 'A is bigger than B' }
  290.        Readln;                                       { Waits for RETURN }
  291.   End.                                               { Ends the program }
  292.  
  293.     Do you understand it now ?! this is quite easy, after you experiment with
  294.   it. Note that you can compare more than just numbers... You can also compare
  295.   strings... It's just the same thing. For example:
  296.  
  297.     'Spellcaster' > 'Diogo' would return TRUE, because 'S' has bigger ASCII
  298.                             value than 'D' (More on ASCII in next article).
  299.  
  300.     'Spellcaster' > ' Saturday' would return TRUE, because they have the same
  301.                                 first letter, so Pascal compares the next one,
  302.                                 and 'a' is smaller than 'p'.
  303.  
  304.     You can also compare variables with fixed values, like this:
  305.  
  306.     A:=10;
  307.     If A>5 Then Write('Ok');
  308.  
  309.     So, there's a lot of flexability.
  310.     Remember in first issue, when I said that there were lots os diferent
  311.   programing 'blocks', started with BEGIN and finished with END ?
  312.   Well, conditions are one of those blocks. you can put more than one command
  313.   in an IF sentence, you just have to precede it with BEGIN and end it with END.
  314.   Let's see:
  315.  
  316.   ONECHAR:='A';
  317.   .......
  318.   .......
  319.   If ONECHAR<'B' Then
  320.   Begin
  321.        ONECHAR:='B';
  322.        Writeln(ONECHAR);
  323.   End;
  324.   .......
  325.   .......
  326.  
  327.     Notice that the END in the IF sentence has a ';' after it, instead of a '.'.
  328.   This is because the '.' is only used in the main block.
  329.  
  330.     Experiment at will... You will see how fun this is... Try to improve the
  331.   program I gave last issue to let you choose what operation will be performed.
  332.   Next issue, I'll give you that program, so that you can compare with your own.
  333.  
  334.  
  335.  
  336.  
  337. ■ The mistery of the lost ASCII
  338.  
  339.     ASCII stands for American Standart Code for Information Interchange. It was
  340.   created a long time ago as a convenctional character sistem.
  341.     When you start your computer, it is loaded from it's internal sistem to the
  342.   memory a series of simbols, that define the letters and numbers you see
  343.   displayed on the screen. In the beginning, there were only 128 simbols, but
  344.   soon they were extended, to create the ASCII-Extended code (which is the one
  345.   we really use and that we refer as ASCII), that has 256 simbols.
  346.     You probably don't understand what am I talking about, so I'll try to
  347.   explain myself a little better.
  348.  
  349.     ASCII is an internal table, a place were there is a series of numbers, from
  350.   0 to 255, and each of those numbers are associated with a letter, a number or
  351.   a simbol. So, you have the ASCII table, where all simbols have a number and
  352.   all numbers have a simbol.
  353.  
  354.     Whats the use, you may ask. There are many uses, but the main one (at least
  355.   that I can think off now) is the comparision of strings.
  356.     There are two functions for you to use in Pascal, to use ASCII directly;
  357.   CHR and ORD.  Their sintaxes are:
  358.  
  359.       =CHR(A) -> Gives you the char with the ASCII code A.
  360.  
  361.       =ORD(A) -> Gives you the ASCII code of the character A.
  362.  
  363.     For a change, let's see an example:
  364.  
  365.  
  366.     Program Test_7;
  367.  
  368.     Var A:Char;
  369.         B:Byte;
  370.  
  371.     Begin
  372.          A:='A';
  373.          B:=Ord(A);
  374.          Writeln('The ASCII code of ',A,' is ',B);
  375.          B:=66;
  376.          A:=Chr(B);
  377.          Writeln('The char with ASCII code of ',B,' is ',A);
  378.          Readln;
  379.     End.
  380.  
  381.     I won't comment this program, so study it carefully. If you have any doubts,
  382.   write to me and I'll try to answear them.
  383.  
  384.  
  385.  
  386. ■ Assembly and Pascal
  387.  
  388.     This issue is dedicated to those who already know how to program, so, if you
  389.   are a begginner, I recomend you don't read this. Read some more issues of
  390.   'The Mag' and then return to this one.
  391.  
  392.   ■ What is Assembly ?
  393.  
  394.     As I said in the last issue, Pascal (and any programming language) is a
  395.   'translator', that transforms the commands that look a bit like standart
  396.   English to binary codes that the machine can understand. Well, Pascal is a
  397.   high-level language, because it has lots of resources and has lots of
  398.   flexability. We just have to worry about using the commands that are available
  399.   to us. Assembly is also a programming language, but a low-level one. You have
  400.   very little commands to work with, because it uses the commands available to
  401.   the machine, and you have to build new commands from them. It's very harder
  402.   working with Assembly, and it requires a good knowledge of what's happening
  403.   inside the computer, and how does he work.
  404.     The main advantage of Asssembly is the speed and size advantage. Any
  405.   program made in Assembly will be faster and shorter than a Pascal or any
  406.   other language made program.
  407.     By luck, Pascal can work with Assembly, by using something called a inline
  408.   assembler. This means that you can do a Pascal program with assembly in the
  409.   middle, for the sake of speed or just because assembly can do things Pascal
  410.   can't.
  411.     To use Assembly in Pascal, you just have to write the word Asm before the
  412.   assembler block, and then write End in the end of that block. Example:
  413.  
  414.   Writeln('Anything...');
  415.   ......
  416.   ......
  417.   Asm
  418.      Mov Ax,10h
  419.      Mov Bx,15h
  420.      Shr Ax
  421.   End;
  422.   ......
  423.   ......
  424.  
  425.     The commands that are between the Asm and the End keywords are Assembly
  426.   commands. Don't forget that almost everything that is in Assembly is in
  427.   hexadecimal.
  428.  
  429.     I'm teaching this, because I will have to use assembler in the next article,
  430.   and I didn't wanted to leave in the dark. I only have to teach some more
  431.   things about the machine and we can start learning how to draw graphics.
  432.  
  433.   ■ The BIOS
  434.  
  435.     The BIOS is the Basic Input/Output Sistem. It's something that resides
  436.   somewhere inside your computer, and that handles the basic input/output (I/O)
  437.   of the computer. It handles the keyboard, the mouse, the screen and all sorts
  438.   of things. Inside of it, there are some commands, called the...
  439.  
  440.   ■ Interrupts
  441.  
  442.     The interrupts are a series of little programs, built in the BIOS. Programs
  443.   that handle all sorts of things. They are very important to make a lot of
  444.   things. All of them can be programmed in Assembly, and you could even forget
  445.   the BIOS, but there are some things that aren't worth the trouble of coding.
  446.     The interrupts are called like this, because when they are executed, they
  447.   stop anything that is going on the processor. Some of the interrupts must be
  448.   called by us, some of them are called by other interrupts, and another ones
  449.   are called a fixed number of times a second. For example, interrupt 8, that
  450.   handles the internal clock is called 18 times a second.
  451.     There are all sorts of ways to call a interrupt from Pascal, but if you have
  452.   Pascal 6 or upwards, you can use the simpler way:
  453.  
  454.   Asm
  455.      Int xx ; where xx is the number of the interrupt.
  456.   End;
  457.  
  458.     Some of the interrupts require parameters to work, and that usually envolves
  459.   calling the interrupt with the register AX with the number of the
  460.   sub-function. Don't take notice of this. I'll explain this in some issue.
  461.  
  462.   ■ Segments and Offsets
  463.  
  464.     One of the more annoying thing about PC's is that it's memory is divided in
  465.   little 64Kb segments. So, everytime you awnt to access a part of the memory,
  466.   you got to specify the segment and the offset.
  467.     Think of it like this: the segment is the page number of the data, while the
  468.   offset is how far into the page is the data you want to access. In standart
  469.   notation this is something like:
  470.  
  471.                 Segment:Offset
  472.  
  473.     Examples:
  474.  
  475.                 A000:0000   -> By luck, this adresses the upper left corner
  476.                                of the VGA screen (of course in hexadecimal)
  477.  
  478.                 B000:0000   -> This adresses the upper left corner of the
  479.                                EGA screen
  480.  
  481.                 54AB:132A   -> This points to the $132A element of page $54AB
  482.  
  483.  
  484.     Don't forget... This is a simplistic view of Segments and Offsets, so don't
  485.   take what I say here for granted.
  486.  
  487.  
  488.  
  489.  
  490. ■ Graphics, part I : Mode 13h
  491.  
  492.     This series of articles is aimmed to the more advanced users, so, if you're
  493.   a begginer, read some more issues of 'The Mag' and then come back to this
  494.   one.
  495.  
  496.   ■ Introduction
  497.  
  498.     If you do programming in Pascal or in C for sometime now, you'll probably
  499.   now how to make graphics, using commands like Initgraph. But, you'll also
  500.   should notice this:
  501.  
  502.   1. You have the need for BGI drivers for any resolution you want.
  503.   2. They are so slow.
  504.   3. They aren't flexible enough.
  505.   4. You don't know what's going on.
  506.  
  507.     So, if you wanted to do, say, a game, you would have to get a BGI driver for
  508.   MCGA graphics (because that's what most games today use), and you would soon
  509.   find out that a game would be unpraticable, because the graphics would be too
  510.   slow. That's the same reason you couldn't do a demo.
  511.  
  512.     I hear you cry now: 'But what's the other way to make graphics ?'.
  513.     My answear is very simple: Assembly language, the BIOS and a lot o clever
  514.   thinking.
  515.  
  516.   ■ What's mode 13h ?
  517.  
  518.     Mode 13h (this is mode 13 hexadecimal, that is the same as mode 19) is a
  519.   graphics mode that gives you a resolution of 320 pixels wide for 200 pixels
  520.   high, with 256 colors. It's the highest resolution the BIOS can directly
  521.   initialize, without need to tweak the VGA registers (more on that later), with
  522.   a VGA card.
  523.  
  524.   ■ How do I get into mode 13h ?
  525.  
  526.     To initialize mode 13h, you just have to execute interrupt 10h,
  527.   sub-function 0, with the parameter 13h. You do it like this:
  528.  
  529.   Procedure Initgraph; Assembler;    { This creates the assembler procedure }
  530.   Asm                                { This starts the assembler (asm) code }
  531.      mov ah,0                        { This loads the subfunction number }
  532.      mov al,13h                      { This loads the graphic mode }
  533.      int 10h                         { This executes the interrupt }
  534.   End;                               { This ends the procedure }
  535.  
  536.     To exit from this mode, you do the same, but change the 13h to 03h:
  537.  
  538.   Procedure Closegraph; Assembler;
  539.   Asm
  540.      mov ah,0
  541.      mov al,03h
  542.      int 10h
  543.   End;
  544.  
  545.     Ok, you know how to get into and out of mode 13h. Now, let's do something...
  546.  
  547.   ■ Putting down a pixel
  548.  
  549.     There are an infinite way of doing this, but I'll teach you two ways. The
  550.   first is using the BIOS. To plot a pixel using the BIOS, you use interrupt
  551.   10h (if you didn't understood yet, interrupt 10h is the video interrupt),
  552.   sub-function 0Ch. You do it like this:
  553.  
  554.   Procedure Putpixel(x,y:word;col:byte); Assembler;
  555.   Asm
  556.      mov ah,0Ch         { Sets the number of the subfunction }
  557.      mov al,[col]       { Sets the color }
  558.      mov cx,[x]         { Sets the x coordinate }
  559.      mov dx,[y]         { Sets the y coordinate }
  560.      mov bx,[1]         { See note below }
  561.      int 10h            { Executes the interrupt - puts the pixel down }
  562.   End;
  563.  
  564.   Note: the 'mov bx,[1]' instruction is there because, in other video modes,
  565.         like EGA, there is more than one display page. The number inside
  566.         brackets is the display page number. In mode 13h, there's only one.
  567.  
  568.     This works fine, but it has a big defect: it's very, very slow. Why ?
  569.   Because it uses interrupts, and interrupts are slow. They are ok for getting
  570.   us into mode 13h, but they are useless for pixel putting.
  571.     This brings us to the other method of putting down a pixel. This is where
  572.   the cleaver thinking comes into work.
  573.  
  574.     First thing you must know is that the video's segment adress is $A000. This
  575.   is very important and you must know this to do something in mode 13h.
  576.     One of the caracteristics of mode 13h is that the video memory is linear.
  577.   That is,the first pixel (the upper left corner one) is at address (A000:0000),
  578.   the second is at (A000:0001), and so on. When you get to the last one of the
  579.   first line, the one that is at (319,0) and has the address (A000:013F), it
  580.   changes line, so pixel (0,1) has the address (A000:0140). I know this is a
  581.   little hard at first, but it will become clear with the following diagram:
  582.  
  583.     (0,0)┌────┬────┬────┬────┬────┬────┬────┬────┬────┬────┬────┬────┐(319,0)
  584.          │0000│0001│0002│0003│0004│....│....│013B│013C│013D│013E│013F│
  585.          ├────┼────┼────┼────┼────┼────┼────┼────┼────┼────┼────┼────┤
  586.          │0140│0141│0142│0143│0144│....│....│....│....│....│....│....│
  587.          ├────┼────┼────┼────┼────┼────┼────┼────┼────┼────┼────┼────┤
  588.          │0280│....│....│....│....│....│....│....│....│....│....│....│
  589.          ├────┼────┼────┼────┼────┼────┼────┼────┼────┼────┼────┼────┤
  590.          │....│....│....│....│....│....│....│....│....│....│....│....│
  591.          ├────┼────┼────┼────┼────┼────┼────┼────┼────┼────┼────┼────┤
  592.          │F8C0│F8C1│F8C2│F8C3│F8C4│....│....│F9FB│F9FC│F9FD│F9FE│F9FF│
  593.   (0,199)└────┴────┴────┴────┴────┴────┴────┴────┴────┴────┴────┴────┘(319,199)
  594.  
  595.     Do you understand it now what I mean by linear adressing. This is a little
  596.   harder to program, but is very easy just the same. To access directly the
  597.   memory, you just have to use the MEM array. The MEM array is a Pascal
  598.   generated array in which all elements have a correspondency in the memory.
  599.   You can write to it as well as you write to. The sintax is as follows:
  600.  
  601.                 MEM[segment:offset]
  602.  
  603.     So, you just have to change the segment to $A000 and find out the offset,
  604.   based in the x and y coordinates. Remember one thing, the screen has 320
  605.   pixels wide and 200 pixels high, so, the formula to find out the offset is
  606.   like this:
  607.  
  608.             Offset:=(Y*320)+X;
  609.  
  610.     For every time we go down a line, we have to increment 320 the offset,
  611.   because we are using linear addressing. So, we would get a procedure like
  612.   this:
  613.  
  614.   Procedure Putpixel(x,y:word;col:byte);
  615.   Begin
  616.        Mem[$A000:(y*320)+x]:=col;
  617.   End;
  618.  
  619.     And voilá... You have a pixel in the screen. Altough this procedure isn't
  620.   in Assembly, it's much faster than the one that is, just because it uses
  621.   direct access to the memory. To see the diference, type in the following
  622.   program:
  623.  
  624.   Program Test_7;
  625.  
  626.   Const VGA=$A000;                   { I prefer defining this constant, }
  627.                                      { instead of writing $A000 milions }
  628.                                      { of times... }
  629.   Var A,B:Word;
  630.  
  631.   Procedure Initgraph; Assembler;    { This creates the assembler procedure }
  632.   Asm                                { This starts the assembler (asm) code }
  633.      mov ah,0                        { This loads the subfunction number }
  634.      mov al,13h                      { This loads the graphic mode }
  635.      int 10h                         { This executes the interrupt }
  636.   End;                               { This ends the procedure }
  637.  
  638.   Procedure Closegraph; Assembler;   { This creates the assembler procedure }
  639.   Asm                                { This starts the assembler (asm) code }
  640.      mov ah,0                        { This loads the subfunction number }
  641.      mov al,03h                      { This loads the graphic mode }
  642.      int 10h                         { This executes the interrupt }
  643.   End;                               { This ends the procedure }
  644.  
  645.   Procedure BIOSPutpixel(x,y:word;col:byte); Assembler;
  646.   Asm
  647.      mov ah,0Ch         { Sets the number of the subfunction }
  648.      mov al,[col]       { Sets the color }
  649.      mov cx,[x]         { Sets the x coordinate }
  650.      mov dx,[y]         { Sets the y coordinate }
  651.      mov bx,[1]         { See note below }
  652.      int 10h            { Executes the interrupt - puts the pixel down }
  653.   End;
  654.  
  655.   Procedure MEMPutpixel(x,y:word;col:byte);
  656.   Begin
  657.        Mem[$A000:(y*320)+x]:=col;       { Puts down a pixel using direct }
  658.   End;                                  { memory access. }
  659.  
  660.   Procedure Cls(col:byte);                     { Clears the screen with }
  661.   Begin                                        { specified color. }
  662.        Fillchar(Mem[$A000:0000],64000,col);    { See note below. }
  663.   End;
  664.  
  665.   Begin
  666.        Writeln('This program tests the two putpixels.');
  667.        Writeln;
  668.        Writeln('First it tests the BIOS Put Pixel, then it');
  669.        Writeln('tests the MEM Put Pixel.');
  670.        Writeln;
  671.        Writeln('It was writen by Spellcaster');
  672.        Writeln('Press Return to start');
  673.        Readln;
  674.        Initgraph;
  675.        For A:=0 to 199 Do
  676.            For B:=0 to 319 Do
  677.                BIOSPutpixel(B,A,Random(255));
  678.        Readln;
  679.        Cls(0);
  680.        For A:=0 to 199 Do
  681.            For B:=0 to 319 Do
  682.                MEMPutpixel(B,A,Random(255));
  683.        Readln;
  684.        Closegraph;
  685.        Writeln('That''s all folks...');
  686.        Readln;
  687.   End.
  688.  
  689.     If you run this program, you will notice the diference in speed between the
  690.   two putpixels. The MEM putpixel is more than two times faster !
  691.  
  692.     If you look at the program, you will notice a procedure I didn't talked
  693.   about: the CLS procedure. It's very easy, this one. It just puts 64000 pixels
  694.   of the specified color, starting in address ($A000:0000). If you think for
  695.   a minute, you'll come to the conclusion that 320*200=64000, that is the number
  696.   of pixels you write. The only thing weird about that procedure is the Fillchar
  697.   keyword. It's sintax is:
  698.  
  699.                 Fillchar(location,nbytes, value)
  700.  
  701.     It fill a certain part of the memory, starting in the defined location,
  702.   with the value specified. It changes the nbytes from the location forward.
  703.   As the memory in mode 13h is linear, we here have a very quick way to clear
  704.   the screen.
  705.  
  706.     I think what I explained here can keep you going for some time. Don't stop
  707.   at simple dots... try to do something, like lines and circles. It's very
  708.   easy, if you try.
  709.  
  710.     In the next issue, in the Graphics feature, I will talk about the pallete,
  711.   one of the more fun things in graphics.
  712.  
  713. ■ Points of view
  714.  
  715.     In this issues points of view, I will have a moan.
  716.  
  717.     Why is information so hard to get ?
  718.     Even if you have a modem and access to the Internet, you will find it almost
  719.   impossible to find something in particular, making the ride in the Net just
  720.   a chance of luck. If you don't have a modem, you just can wait one of three
  721.   things: Friends that know something they can teach you, friends that have the
  722.   information you require, and the purchase of books. In the last case, good
  723.   books are so hard to find (at least here), and they are so expensive. Some
  724.   of them (like Peter Norton's Guides) cost near 15.000$00 (something like
  725.   $100 US or £60 UK). People like me don't have that kind of money to spend
  726.   on some book that only says a couple of things of interest. Even the books
  727.   by Peter Norton are full of things that doesn't matter for the ordinary
  728.   programmer.
  729.     That is one of the reasons that pushed me in making 'The Mag'. The lack of
  730.   informations. I have lost more than three years gathering the knowledge I
  731.   have (I would know more if I've always been a PC person, but I was for many
  732.   years a Amiga person), and I now give it free of charge to you (but if you
  733.   want to pay me, go ahead. You'll get my undying thanks).
  734.     So, until someone decides to write a really good book about programming, at
  735.   an affordable price, I'll make this magazine, and make it grow. I hope that
  736.   in one year, I'll have some contributors to write some of the articles, and
  737.   that you learn enough to give me the possibility to write more advanced stuff,
  738.   like 3D graphics, texture mapping and other of those new flashy techniques.
  739.   I really hope to get some contributors, because I have limited knowledge.
  740.   I can teach you many things, but at the rate I'm going, in one year you'll
  741.   know as much as me.
  742.     So, people, goodbye... Stay well... Until the next issue. Experiment, try
  743.   out, and do some other stuff do.
  744.  
  745.  
  746.  
  747. ■ The adventures of Spellcaster, the rebel programmer of the year 2018.
  748.  
  749.   Episode 2 - Cry for freddom
  750.  
  751.     I hear a scream in the far. I don't mind... I was getting used to it. It
  752.   must have been some poor bastard who didn't survived this isolation from PCs,
  753.   and cracked up...
  754.     I was decided not to let that happen to me... I would survive enough to get
  755.   out of here and try to push the system down. But I was a little short of
  756.   ideas, until I remembered a small 'joke' I puled in High-Scholl, just two
  757.   years ago. I've had, back then, gotten my first PC, and I decided to program
  758.   a virus, just to play. So I did, but then, the virus ran out of control and
  759.   erased almost my entire hard-drive. I managed, with some effort, to isolate
  760.   it in a disk and I put it away. Some days after, it was the end of my 10th
  761.   grade. I was in front of my Programming Practices teacher, who looked at me
  762.   and said that I was going to get a lower grade than I expected. I was furious,
  763.   because I deserved that grade, so I ploted a sinister revenge. Late at night,
  764.   I broke in the scholl's computer center, carrying only a disk. I inserted
  765.   the disk, and a prompt appeared on the screen:
  766.   'Do you really want to release the Final Judgement Virus (Y/N) ?'.
  767.   I pressed the 'y' key, and in the seconds, the whole network was infected.
  768.   I returned home, laughing to myself, and went to bed. The day after, I went
  769.   to scholl, only to find it in chaos and anarchy. Everything had broken down,
  770.   from the grading database to the students data. I smilled with a grin...
  771.     Remembering this, I started to think of my escape. The only problem was that
  772.   we only had access to Atari 520ST computers, something from the previous
  773.   century, an old machine that didn't work properly even when it was lauched.
  774.   But, winning the fight against my sense of filth and disgust, I grabed a
  775.   book and started to read. It was called:'How to program the Atari'.
  776.  
  777.  
  778.                                          See you in the next issue
  779.                                         Diogo "SpellCaster" Andrade
  780.